package com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement;

import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Attachment;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Person;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知公告基础信息
 *
 * @author jiangqinxuan
 * &#064;date  2021/07/26
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Announcement {
    /**
     * id
     */
    private Long id;
    /**
     * 标题
     */
    private String title;
    /**
     * 简介
     */
    private String content;
    /**
     * 通知公告发布人
     */
    private Person publisher;
    /**
     * 通知公告接收人
     */
    private List<Receiver> receivers;
    /**
     * 审核人
     */
    private Person reviewer;
    /**
     * 流程任务
     */
    private Task task;
    /**
     * 附件
     */
    private List<Attachment> attachments;
    /**
     * 发布时间
     */
    private LocalDateTime publishDateTime;
    /**
     * 审核时间
     */
    private LocalDateTime reviewDateTime;
    /**
     * 创建时间
     */
    private LocalDateTime createDateTime;
    /**
     * 管理部门/主体院统计
     */
    private List<Statistics> statisticsInInstitutionAndOrganization;
    /**
     * 管理部门统计
     */
    private List<Statistics> statisticsInInstitution;
    /**
     * 主体院统计
     */
    private List<Statistics> statisticsInOrganization;
    /**
     * 项目统计
     */
    private List<StatisticsInProject> statisticsInProject;
    /**
     * 是否限内部
     */
    private Boolean internal;
    /**
     * 总体 总接收人数
     */
    private int receiverTotalNum;
    /**
     * 总体 已读接收人数
     */
    private int readReceiverNum;
    /**
     * 总体 未读接收人数
     */
    private int noReadReceiverNum;
    /**
     * 管理部门/主体院 总接收人数
     */
    private int receiverTotalNumInInstitutionAndOrganization;
    /**
     * 管理部门/主体院 已读接收人数
     */
    private int readReceiverNumInInstitutionAndOrganization;
    /**
     * 管理部门/主体院 未读接收人数
     */
    private int noReadReceiverNumInInstitutionAndOrganization;
    /**
     * 管理部门 总接收人数
     */
    private int receiverTotalNumInInstitution;
    /**
     * 管理部门 已读接收人数
     */
    private int readReceiverNumInInstitution;
    /**
     * 管理部门 未读接收人数
     */
    private int noReadReceiverNumInInstitution;
    /**
     * 主体院 总接收人数
     */
    private int receiverTotalNumInOrganization;
    /**
     * 主体院 已读接收人数
     */
    private int readReceiverNumInOrganization;
    /**
     * 主体院 未读接收人数
     */
    private int noReadReceiverNumInOrganization;
    /**
     * 项目 总接收人数
     */
    private int receiverTotalNumInProject;
    /**
     * 项目 已读接收人数
     */
    private int readReceiverNumInProject;
    /**
     * 项目 未读接收人数
     */
    private int noReadReceiverNumInProject;
    /**
     * 当前登录人是否已读
     */
    private Boolean readByCurrentUser;
    /**
     * 通知公告类型
     */
    private String type;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Statistics {

        private String name;
        private Long sourceFromId;
        private String sourceFromType;
        private Integer displayOrder1;
        private Integer displayOrder2;
        private List<Receiver> receivers = new ArrayList<>();
        private int receiverTotalNum;
        private int readReceiverNum;
        private int noReadReceiverNum;

        public Statistics(String name) {
            this.name = name;
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StatisticsInProject {
        private String name;
        private Integer displayOrder;
        private List<Statistics> projectStatistics;

        public StatisticsInProject(String name) {
            this.name = name;
        }

        public Integer getDisplayOrder() {
            return this.projectStatistics.get(0).displayOrder1;
        }
    }
}